﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LetiLib.Data;
using LetiLib.Utils;

namespace LedCenter.GUI.PopupControls
{
    public abstract partial class PopupControl : UserControl
    {
        public PopupControl()
        {
            InitializeComponent();
        }

        public PopupControl(POPUP p, bool editMode = false)
        {
            InitializeComponent();
            isUpdateMode = editMode;
            txtX.Enabled = isUpdateMode;
            txtY.Enabled = isUpdateMode;
            txtZ.Enabled = isUpdateMode;
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Enabled = isUpdateMode;
            cbType.Enabled = isUpdateMode;

            btnCancel.Visible = btnOK.Visible = false;
            _popup = p;
            cbType.DataSource = Enum.GetValues(typeof(POPUP_TYPE));
            
            txtX.Text = p.X.ToString();
            txtY.Text = p.Y.ToString();
            txtZ.Text = p.Z.ToString();
            txtWidth.Text = p.Width.ToString();
            txtHeight.Text = p.Height.ToString();
            cbType.SelectedIndex = Array.IndexOf(Enum.GetValues(typeof(POPUP_TYPE)), p.DataType);
        }

        protected void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        public POPUP _popup;

        protected virtual void btnOK_Click(object sender, EventArgs e)
        {
            if (_popup.DataType != (POPUP_TYPE)Enum.Parse(typeof(POPUP_TYPE), cbType.SelectedItem.ToString()))
            {
                ushort oldID = _popup.ID;
                _popup = EnumUtils.getPopupByType((POPUP_TYPE)Enum.Parse(typeof(POPUP_TYPE), cbType.SelectedItem.ToString()));
                _popup.ID = oldID;
            }
            _popup.X = ushort.Parse(txtX.Text);
            _popup.Y = ushort.Parse(txtY.Text);
            _popup.Z = byte.Parse(txtZ.Text);
            _popup.Width = ushort.Parse(txtWidth.Text);
            _popup.Height = ushort.Parse(txtHeight.Text);
            _popup.DataType = (POPUP_TYPE)Enum.Parse(typeof(POPUP_TYPE), cbType.SelectedItem.ToString());
            _popup.StartDate = new DATETIME();
            _popup.EndDate = new DATETIME();
            //_popup.ItemCount = 0;
            //_popup.ActiveItem = 0;

            if (isUpdateMode)
            {
                isUpdateMode = false;

                btnUpdate.Visible = !isUpdateMode;
                btnCancel.Visible = btnOK.Visible = isUpdateMode;
                if (OnPopupUpdate != null)
                    OnPopupUpdate(this);

            }
        }

        protected bool isUpdateMode = false;
        public delegate void UpdatePopupHandler(object sender);
        public event UpdatePopupHandler OnPopupUpdate;

        //The event-invoking method that derived classes can override.
        protected virtual void OnPopupUpdated(object sender)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.

            if (this.OnPopupUpdate != null)
                this.OnPopupUpdate(this);
        }

        protected virtual void btnUpdate_Click(object sender, EventArgs e)
        {
            
            isUpdateMode = true;

            btnUpdate.Visible = !isUpdateMode;
            btnCancel.Visible = btnOK.Visible = isUpdateMode;
            txtX.Enabled = isUpdateMode;
            txtY.Enabled = isUpdateMode;
            txtZ.Enabled = isUpdateMode;
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Enabled = isUpdateMode;
            cbType.Enabled = isUpdateMode;
        }

        protected virtual void btnCancel_Click(object sender, EventArgs e)
        {
            isUpdateMode = false;

            btnUpdate.Visible = !isUpdateMode;
            btnCancel.Visible = btnOK.Visible = isUpdateMode;

            txtX.Text = _popup.X.ToString();
            txtX.Enabled = isUpdateMode;
            txtY.Text = _popup.Y.ToString();
            txtY.Enabled = isUpdateMode;
            txtZ.Text = _popup.Z.ToString();
            txtZ.Enabled = isUpdateMode;
            txtWidth.Text = _popup.Width.ToString();
            txtWidth.Enabled = isUpdateMode;
            txtHeight.Text = _popup.Height.ToString();
            txtHeight.Enabled = isUpdateMode;
            cbType.SelectedItem = _popup.DataType;
            cbType.Enabled = isUpdateMode;
        }
    }

    public class MiddlePopupControl : PopupControl
    {
        public MiddlePopupControl()
            : base()
        {

        }

        public MiddlePopupControl(POPUP p, bool editMode = false):base(p,editMode)
        {
        
        }
    }

}
